<template>
  <section class="todoapp">
    <TodoHeader :arr="list" @add="addFn"></TodoHeader>
    <TodoMain :arr="showList" @del="delFn"></TodoMain>
    <TodoFooter
      :arr="list"
      @changeType="changeTypeFn"
      @clearDone="clearDoneFn"
    ></TodoFooter>
  </section>
</template>

<script>
// 样式引入 + 导入组件并注册使用
import "./styles/base.css";
import "./styles/index.css";
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
import TodoFooter from "./components/TodoFooter.vue";
export default {
  watch: {
    list: {
      deep: true,
      handler(newVal) {
        localStorage.setItem(JSON.stringify("todos", newVal));
      },
    },
  },
  computed: {
    showList() {
      if (this.type === "active") {
        return this.list.filter((item) => !item.isDone);
      } else if (this.type === "ok") {
        return this.list.filter((item) => item.isDone);
      } else {
        return this.list;
      }
    },
  },
  methods: {
    clearDoneFn() {
      this.list = this.list.filter((item) => !item.isDone);
    },
    changeTypeFn(type) {
      this.type = type;
    },
    addFn(name) {
      this.list.unshift({
        id: +new Date(),
        name,
        isDone: false,
      });
    },
    delFn(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
  },
  data() {
    return {
      type: "",
      list: JSON.parse(localStorage.getItem("todos")) || [],
      // list: [
      //   {
      //     id: 100,
      //     // name 就是需要完成的任务
      //     name: "吃饭",
      //     // isDone 表示任务是否完成
      //     isDone: true,
      //   },
      //   {
      //     id: 201,
      //     name: "睡觉",
      //     isDone: false,
      //   },
      //   {
      //     id: 103,
      //     name: "打豆豆",
      //     isDone: true,
      //   },
      // ],
    };
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
};
</script>
